package cn.lili.modules.order.order.entity.dos;

import cn.lili.common.utils.BeanUtil;
import cn.lili.common.utils.SnowFlake;
import cn.lili.modules.order.aftersale.entity.dos.AfterSale;
import cn.lili.mybatis.model.BaseEntity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serial;
import java.util.Date;

/**
 * 商家订单流水
 *
 * @author Chopper
 * @since 2020/11/17 7:31 下午
 */
@Data
@TableName("li_refund_flow")
@ApiModel(value = "退款流水")
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
public class RefundFlow extends BaseEntity {

    @Serial
    private static final long serialVersionUID = 8485705803992262968L;

    @ApiModelProperty(value = "退单编号")
    private String afterSaleNo;

    @ApiModelProperty(value = "订单编号")
    private String orderSn;

    @ApiModelProperty(value = "金额")
    private Double refundPrice;

    @ApiModelProperty(value = "支付金额")
    private Double payPrice;

    @ApiModelProperty(value = "是否已退款")
    private Boolean refunded;

    @ApiModelProperty(value = "退款方式")
    private String paymentMethod;


    @ApiModelProperty(value = "第三方支付发起交易号")
    private String outTradeNo;

    @ApiModelProperty(value = "第三方平台付款流水号")
    private String transactionId;


    @ApiModelProperty(value = "第三方退款请求流水")
    private String outRefundNo;

    @ApiModelProperty(value = "第三方平台退款流水号")
    private String refundTransactionId;


    @ApiModelProperty(value = "第三方退款流水号")
    private String receivableNo;

    @ApiModelProperty(value = "退款理由")
    private String refundReason;

    @ApiModelProperty(value = "退款失败原因")
    private String errorMessage;


    @ApiModelProperty(value = "会员id")
    private String memberId;

    @ApiModelProperty(value = "会员名称")
    private String memberName;

    @ApiModelProperty(value = "店铺ID")
    private String storeId;
    @ApiModelProperty(value = "店铺名称 ")
    private String storeName;

    @ApiModelProperty(value = "供应商标识")
    private String supplierId;
    @ApiModelProperty(value = "供应商名称")
    private String supplierName;

    @ApiModelProperty(value = "分销商标识")
    private String distributorId;

    @ApiModelProperty(value = "分销商标识")
    private String distributorName;

    @CreatedDate
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(fill = FieldFill.INSERT)
    @ApiModelProperty(value = "创建时间", hidden = true)
    private Date createTime;


    //用户支付金额=平台佣金+供应商金额+店铺金额+分销佣金
    public RefundFlow(AfterSale afterSale, OrderItem orderItem, Order order) {
        BeanUtil.copyProperties(orderItem, this);

        //模型字段适配
        this.orderSn = order.getSn();
        this.refundReason = afterSale.getReason();
        this.refundPrice = afterSale.getActualRefundPrice();
        this.memberId = afterSale.getMemberId();
        this.payPrice = orderItem.getFlowPrice();
        this.refunded = false;
        this.memberName = afterSale.getMemberName();

        this.setAfterSaleNo(afterSale.getSn());
        //添加支付方式
        this.setPaymentMethod(order.getPaymentMethod());
        this.setOutTradeNo(order.getOutTradeNo());
        this.setTransactionId(order.getTransactionId());

        //去掉orderitem的时间。
        this.setCreateTime(null);
        this.setId(null);
    }

    //整单退款
    public RefundFlow(Order order) {
        BeanUtil.copyProperties(order, this);

        //模型字段适配
        this.orderSn = order.getSn();
        this.refundReason = order.getCancelReason();
        this.refundPrice = order.getFlowPrice();
        this.memberId = order.getBuyerId();
        this.payPrice = order.getFlowPrice();
        this.refunded = false;
        this.memberName = order.getNickname();

        this.setAfterSaleNo(SnowFlake.createStr("AF"));
        //添加支付方式
        this.setPaymentMethod(order.getPaymentMethod());

        //去掉orderitem的时间。
        this.setCreateTime(null);
    }
}